package edu.cuit.campus.company.service.bean;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.cuit.campus.commons.StringUtils;
import edu.cuit.campus.company.service.CompanyService;
import edu.cuit.campus.entity.po.Company;
import edu.cuit.campus.repository.common.MybatisHelper;
import edu.cuit.campus.repository.mapper.CompanyMapper;

/**
 * 企业业务实现类
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/11 14:03
 */
public class CompanyServiceBean implements CompanyService {
    @Override
    public Integer register(Company company) {
        if(!StringUtils.isBlank(company.getPassword()))
            company.setPassword(StringUtils.hash(company.getPassword()));
        var session = MybatisHelper.openSession();
        var mapper = session.getMapper(CompanyMapper.class);
        mapper.insert(company);
        session.commit();
        MybatisHelper.close();
        return company.getId();
    }

    @Override
    public PageInfo<Company> list(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        var session = MybatisHelper.openSession();
        var mapper = session.getMapper(CompanyMapper.class);
        var result = mapper.limit();
        MybatisHelper.close();
        return new PageInfo<>(result);
    }
}
